//
//  YDFundDetailsController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/14.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDFundDetailsController.h"
#import "YDFundDetailsTopView.h"
#import "YDFundDetailsTableCell.h"
#import "YDFundDetailsTableHeaderView.h"
#import "YDFundDetailsModel.h"
#import "YDBillDetailsController.h"
#import "YDBuyOptionsViewController.h"


@interface YDFundDetailsController ()<UITableViewDelegate , UITableViewDataSource , SYFundDetailsTableHeaderViewDelegate , SYNoDataViewDelegate>
//YDFundDetailsTopView
@property (nonatomic , weak) YDFundDetailsTopView *topView;
//tableView
@property (nonatomic , weak) UITableView *tableView;
@property (nonatomic , assign) NSInteger currentPage;
@property (nonatomic , assign) SYFundDetailsControllerDataStyle dataStyle;
//是不是需要显示筛选条
@property (nonatomic , assign) BOOL isShowSelectedView;

@property (nonatomic , strong) NSMutableArray *modelArray;

//暂无资金明细view
@property (nonatomic, strong)UILabel *noMoneyDetailView;

@property (nonatomic , strong) NSDictionary *dict;

@property (nonatomic , weak) SYNoDataView *dataView;

@end

@implementation YDFundDetailsController

-(NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"资金明细";
//    self.extendedLayoutIncludesOpaqueBars = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = NumberColor(247, 247, 247);
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    self.isShowSelectedView = YES;
//    [self topView];
    [self tableView];
    [self setUpDataManger];
    [self getCurrentFundAccount];
    [self loadData];
}

-(void)setUpDataManger
{

    MJGIFHeader *header = [MJGIFHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES ;
    //隐藏状态
    header.stateLabel.hidden = YES ;
    self.tableView.mj_header = header;
    
    MJGIFFooter *footer = [MJGIFFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.stateLabel.hidden = YES;
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    
}
-(void)getCurrentFundAccount
{
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:FundAccount WithSuccessBlock:^(NSDictionary *resultDict) {
        if ([resultDict[@"code"] integerValue] == 200) {
//            self.topView.dict = resultDict[@"result"];
            self.dict = resultDict[@"result"];
        }
    } failBlock:^(NSError *error) {
        
    }];
}
-(void)loadData
{
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (self.dataStyle == SYFundDetailsControllerDataAll) {
        [dict setObject:@(0) forKey:@"range"];
    }else if (self.dataStyle == SYFundDetailsControllerDataWeek){
        [dict setObject:@(1) forKey:@"range"];
    }else if (self.dataStyle == SYFundDetailsControllerDataMonth){
        [dict setObject:@(2) forKey:@"range"];
    }else{
        [dict setObject:@(3) forKey:@"range"];
    }
    [dict setObject:@(0) forKey:@"page"];
    [dict setObject:@(10) forKey:@"size"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:FundDetails WithSuccessBlock:^(NSDictionary *resultDict) {
        [self.tableView.mj_header endRefreshing];
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        if ([resultDict[@"code"] integerValue] == 200) {
            self.currentPage = [resultDict[@"result"][@"number"] integerValue];
            if (self.modelArray.count) {
                [self.modelArray removeAllObjects];
            }
            self.modelArray = [YDFundDetailsModel mj_objectArrayWithKeyValuesArray:resultDict[@"result"][@"content"]];
            
            [self.tableView reloadData];
            
            if (self.modelArray.count < 10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshing];
            }
            
            if (!self.modelArray.count) {
                //没有数据
                self.tableView.mj_footer.hidden = YES;
                [self dataView];
            }else
            {
                [self removeNoMoenyDetailView];
            }
            
        }else{
            self.tableView.mj_footer.hidden = NO;
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                            WithType:NavTipsViewTypeMessage text:resultDict[@"message"]
                                                          isExistNav:NO];
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = NO;
  
        DDLog(@"%@",error)
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                        WithType:NavTipsViewTypeMessage
                                                            text:kNet_Error_ConnectionLost
                                                      isExistNav:NO];
    }];

}
-(void)loadMoreData
{
    [[SYProgressHUD sharedInstance] showMBHudWithSting:@"加载中..."];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (self.dataStyle == SYFundDetailsControllerDataAll) {
        [dict setObject:@(0) forKey:@"range"];
    }else if (self.dataStyle == SYFundDetailsControllerDataWeek){
        [dict setObject:@(1) forKey:@"range"];
    }else if (self.dataStyle == SYFundDetailsControllerDataMonth){
        [dict setObject:@(2) forKey:@"range"];
    }else{
        [dict setObject:@(3) forKey:@"range"];
    }
    [dict setObject:@(self.currentPage + 1) forKey:@"page"];
    [dict setObject:@(10) forKey:@"size"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:FundDetails WithSuccessBlock:^(NSDictionary *resultDict) {
        [self.tableView.mj_footer endRefreshing];
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        DDLog(@"resultDict = %@" , resultDict);
        if ([resultDict[@"code"] integerValue] == 200) {
            self.currentPage = [resultDict[@"result"][@"number"] integerValue];
            NSArray *array = [YDFundDetailsModel mj_objectArrayWithKeyValuesArray:resultDict[@"result"][@"content"]];
            [self.modelArray addObjectsFromArray:array];
            
            [self.tableView reloadData];
            
            if (array.count < 10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshing];
            }
        }else{
            self.tableView.mj_footer.hidden = NO;
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                            WithType:NavTipsViewTypeMessage text:resultDict[@"message"]
                                                          isExistNav:YES];
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.hidden = NO;
        DDLog(@"%@",error)
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                        WithType:NavTipsViewTypeMessage
                                                            text:kNet_Error_ConnectionLost
                                                      isExistNav:YES];
    }];
}
//设置导航栏按钮
-(void)setUpNavButton
{
    UIBarButtonItem *rightItemBtn = [UIBarButtonItem imageWithNormalImage:[UIImage imageNamed:@"wd-zijinmingxi-shaixuan-b"] selectedImage:[UIImage imageNamed:@"wd-zijinmingxi-shaixuan-a"] addTarget:self action:@selector(rightItemButtonClick:)];
    self.navigationItem.rightBarButtonItem = rightItemBtn;
}

//- (void)removeNoMoenyDetailView
//{
//    if (_noMoneyDetailView)
//    {
//        [_noMoneyDetailView removeFromSuperview];
//        _noMoneyDetailView =nil;
//        self.tableView.mj_footer.hidden = NO;
//    }
//}
- (void)removeNoMoenyDetailView
{
    if (_dataView)
    {
        [_dataView removeFromSuperview];
        _dataView =nil;
        self.tableView.mj_footer.hidden = NO;
    }
}
-(void)rightItemButtonClick:(UIButton *)button
{
    button.selected = !button.selected;
    if (button.selected) {
        self.isShowSelectedView = YES;
        [self.tableView reloadData];
    }else{
        self.isShowSelectedView = NO;
        [self.tableView reloadData];
    }
}

//******YDFundDetailsTopView******////
-(YDFundDetailsTopView *)topView
{
    if (_topView == nil) {
        YDFundDetailsTopView *topView = [YDFundDetailsTopView fundDetailsTopViewWithAccountDict:nil];
        [self.view addSubview:topView];
        if (isPhoneX) {
            topView.frame = CGRectMake(0, 0, LKScreenW, rateX812(160));
        }else{
            topView.frame = CGRectMake(0, 0, LKScreenW, rate750(140));
        }
        _topView = topView;
    }
    return _topView;
}


////*********tableView**********///
-(UITableView *)tableView
{
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, LKScreenH - (isPhoneX ? 88 : 64)) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(247, 247, 247);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

-(UILabel *)noMoneyDetailView
{
    if (!_noMoneyDetailView) {
        _noMoneyDetailView = [UILabel new];
        _noMoneyDetailView.text = @"暂无资金明细";
        _noMoneyDetailView.textColor = [UIColor colorWithHexString:blueyGrey];
        _noMoneyDetailView.font = [UIFont systemFontOfSize:font16ScreenWRate *LKScreenW];
        _noMoneyDetailView.textAlignment = NSTextAlignmentCenter;
        [self.tableView addSubview:_noMoneyDetailView];
        [_noMoneyDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(isPhoneX ? @(rateX812(212)) : @(rate750(212)));
        }];
        
        
    }
    return _noMoneyDetailView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
//
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.isShowSelectedView) {
        return isPhoneX ? rateX812(50) : rate750(50);
    }else{
        return 0.0f;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(56) : rate750(56);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDFundDetailsTableCell *cell = [YDFundDetailsTableCell cellWithTableView:tableView];
    YDFundDetailsModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.isShowSelectedView) {
        YDFundDetailsTableHeaderView *headerView = [YDFundDetailsTableHeaderView headerViewWithTableView:tableView];
        headerView.delegate = self;
        return headerView;
    }else{
        return nil;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.modelArray.count)return;
    YDBillDetailsController *vc = [[YDBillDetailsController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    YDFundDetailsModel *model = self.modelArray[indexPath.row];
    vc.model = model;
//    vc.fundDict = self.topView.dict;
    vc.fundDict = self.dict;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma SYFundDetailsTableHeaderViewDelegate
-(void)fundDetailsTableHeaderView:(YDFundDetailsTableHeaderView *)headerView didClickButtonIndex:(NSInteger)index
{
    DDLog(@"点击选了刷选条件按钮")
    self.dataStyle = index;
    [self loadData];
}




-(SYNoDataView *)dataView
{
    if (_dataView == nil) {
        CGFloat dataW = rate375(240);
        CGFloat dataX = (LKScreenW - dataW) * 0.5;
        CGFloat dataH = isPhoneX ? rateX812(276) : rate750(276);
        CGFloat dataY = isPhoneX ? rateX812(100) : rate750(100);
        CGRect frame = CGRectMake(dataX, dataY, dataW, dataH);
        //wuneirong   wuxinhao
        SYNoDataView *dataView = [SYNoDataView positionNoDataViewInitWithFrame:frame ImageString:@"wuneirong" buttonTitle:@"策略申购" IsCanSeeBuyButton:YES];
        dataView.delegate = self;
        dataView.isCanSeeBuyButton = YES;
        [self.tableView addSubview:dataView];
        _dataView = dataView;
    }
    return _dataView;
}
#pragma SYNoDataViewDelegate
-(void)noDataView:(SYNoDataView *)noDataView didClickSaleButton:(SYNoHeightedButton *)button
{
    YDBuyOptionsViewController *vc = [[YDBuyOptionsViewController alloc] init];
    vc.stockCode = @"000001";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    DDLog(@"期权持仓界面期权交易")
}
@end
